package com.muyu.cloud.commodity.controller;

import com.muyu.cloud.commodity.domain.model.Category;
import com.muyu.cloud.commodity.domain.req.CategoryAddReq;
import com.muyu.cloud.commodity.domain.req.CategoryUpdDateReq;
import com.muyu.cloud.commodity.service.CategoryService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/product/category")
@Tag(name = "CategoryController", description = "商品分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 父级查询子分类
     * @param parentId
     * @return
     */
    @Operation(summary = "按父级查询子分类")
    @RequiresPermissions("product:category:list")
    @GetMapping("/list")
    public Result<List<Category>> list(@RequestParam(value = "parentId", required = false) Long parentId) {
        return Result.success(categoryService.listByParentId(parentId));
    }

    /**
     * 分类树（管理端）
     * @return
     */
    @Operation(summary = "分类树（一级-二级-三级）（管理端）")
    @RequiresPermissions("product:category:tree")
    @GetMapping("/tree")
    public Result<List<Category>> tree() {
        return Result.success(categoryService.tree());
    }

    /**
     * 分类树（公开接口，无需认证）
     * @return
     */
    @Operation(summary = "分类树（公开接口）")
    @GetMapping("/public/tree")
    public Result<List<Category>> publicTree() {
        return Result.success(categoryService.tree());
    }

    /**
     * 添加分类
     * @param req 分类添加请求
     * @return
     */
    @Operation(summary = "添加分类")
    @RequiresPermissions("product:category:add")
    @PostMapping("/add")
    public Result<Void> add(@Valid @RequestBody CategoryAddReq req) {
        categoryService.addCategory(req);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryUpdDateReq
     * @return
     */
    @Operation(summary = "修改分类")
    @RequiresPermissions("product:category:edit")
    @PutMapping("/update")
    public Result<Void>update(@Valid @RequestBody CategoryUpdDateReq categoryUpdDateReq){
        categoryService.update(categoryUpdDateReq);
        return Result.success();
    }



}

